package cn.wolfcode.shop.web.controller;

import cn.wolfcode.shop.domain.*;
import cn.wolfcode.shop.service.*;
import cn.wolfcode.shop.vo.GenerateSkuVO;
import cn.wolfcode.shop.vo.JsonResultVO;
import cn.wolfcode.shop.vo.SaveSkuVO;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class SkuPropertyController {
    @Reference
    private ICatalogService catalogService;
    @Reference
    private IPropertyService propertyService;
    @Reference
    private IPropertyValueService propertyValueService;
    @Reference
    private IProductService productService;
    @Reference
    private ISkuPropertyService skuPropertyService;
    @Reference
    private ISkuPropertyValueService skuPropertyValueService;
    @Reference
    private IProductSkuService productSkuService;


    @RequestMapping("skuProperty")
    public String skuProperty(Model model) {
        List<Catalog> catalogs = catalogService.selectAll();
        model.addAttribute("allCatalog", JSON.toJSONString(catalogs));
        return "sku_property/sku_property";
    }

    /**
     * SKU属性列表
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("skuProperty/get/{id}")
    public String property(@PathVariable Long id, Model model) {
        List<Property> propertyList = propertyService.selectByCatalogId(id);
        model.addAttribute("propertyList", propertyList);

        return "sku_property/sku_property_list";
    }


    /**
     * 商品属性值
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("skuPropertyValue/get/{id}")
    public String propertyValue(@PathVariable Long id, Model model) {
        model.addAttribute("propertyId", id);

        List<PropertyValue> propertyValuesList = propertyValueService.selectByPropertyId(id);
        model.addAttribute("propertyValueList", propertyValuesList);

        return "sku_property/sku_property_value_list";
    }

    /**
     * 保存或修改商品属性值
     *
     * @param propertyValues
     * @return
     */
    @RequestMapping("skuPropertyValue/save")
    @ResponseBody
    public JsonResultVO propertyValueSaveOrUpdate(@RequestBody List<PropertyValue> propertyValues) {
        propertyValueService.saveOrUpdate(propertyValues);
        return JsonResultVO.success();
    }

    /**
     * 商品分类属性编辑
     *
     * @param property
     * @return
     */
    @RequestMapping("skuProperty/save")
    @ResponseBody
    public JsonResultVO propertySave(Property property) {
        propertyService.saveOrUpdateProperty(property);
        return JsonResultVO.success();
    }

    /**
     * 删除商品属性
     *
     * @param id
     * @return
     */
    @RequestMapping("skuProperty/delete/{id}")
    @ResponseBody
    public JsonResultVO propertyDelete(@PathVariable Long id) {
        propertyService.propertyDelete(id);
        return JsonResultVO.success();
    }

    /**
     * 生成sku的主页
     *
     * @param productId
     * @param model
     * @return
     */
    @RequestMapping("productSku")
    public String productSku(Long productId, Model model) {

        Product product = productService.selectByProductId(productId);
        List<SkuProperty> skuProperty= skuPropertyService.selectByCatalogId(product.getCatalog().getId());
        model.addAttribute("product",product );


        //判断商品是否已经创建了sku
        List<ProductSku> productSkus = productSkuService.listByProductId(productId);
        if(productSkus.size() > 0){
            model.addAttribute("skuList",productSkus);

            //商品sku属性值对应的属性名(标题)
            List<SkuProperty> skuPropertyList = new ArrayList<SkuProperty>();

            List<ProductSkuProperty> productSkuProperties =  productSkus.get(0).getProductSkuPropertyList();

            for(ProductSkuProperty productSkuProperty : productSkuProperties ){
                skuPropertyList.add(productSkuProperty.getSkuProperty());
            }
            model.addAttribute("skuPropertyList" , skuPropertyList) ;

            return "product_sku/sku_list";
        }



        model.addAttribute("skuPropertyList",skuProperty );


        return "product_sku/generate_sku";
    }

    /**
     *  共享sku属性的方法
     * @param skuPropertyId
     * @return
     */
    @RequestMapping("productSku/getSkuPropertyValue")
    public String getSkuPropertyValue(Long skuPropertyId,Model model){
        List<SkuPropertyValue> skuPropertyValues= skuPropertyValueService.selectBySKUPropertyId(skuPropertyId);
        SkuProperty skuPropertie = skuPropertyService.selectById(skuPropertyId);
        model.addAttribute("skuPropertyValueList",skuPropertyValues);
        model.addAttribute("skuProperty",skuPropertie);
        return "product_sku/sku_property_value_table";
    }

    /**
     * 生成sku 数据
     * @param generateSkuVO 用于接收网页参数(json格式)
     * @return
     */
    @RequestMapping("productSku/generateSku")
    public String generateSku(@RequestBody GenerateSkuVO generateSkuVO,Model model){
        List<Map<String,Object>> list=skuPropertyService.generateSku(generateSkuVO);
        model.addAttribute("skuPropertyList",generateSkuVO.getSkuPropertyList());
        model.addAttribute("skuResultList",list);
        return "product_sku/sku_result_list";

    }

    /**
     * 保存SKU数据
     * @return
     */
    @RequestMapping("/productSku/save")
    @ResponseBody
    public JsonResultVO productSkuSave(SaveSkuVO saveSkuVO){
        productService.productSkuSave(saveSkuVO);
        return JsonResultVO.success() ;
    }


}
